package com.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Khaled Lela
 * Class used to change PassWord
 */
public class ChangePassSql {

    private Connection con;
    private PreparedStatement ps;
    private ResultSet rs;

    public ChangePassSql() {
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            con = DriverManager.getConnection("jdbc:derby:BreadDB;");
        } catch (Exception ex) {
        }
    }

    public String getOldPass() {
        String oldPass = null;
        try {
            ps = con.prepareStatement("SELECT password FROM login"
                    + " WHERE "
                    + " username = ? ");
            ps.setString(1, "admin");
            rs = ps.executeQuery();
            while (rs.next()) {
                oldPass = rs.getString("password");
            }
        } catch (Exception ex) {
        }
        return oldPass;
    }

    public void setPass(String pass) {
        try {
            ps = con.prepareStatement("UPDATE login "
                    + "SET   password=? "
                    + " WHERE username = ? ");
            ps.setString(1, pass);
            ps.setString(2, "admin");
            ps.executeUpdate();
        } catch (Exception ex) {
        }
    }
}
